package com.gitee.huanminabc.fastboot.load.stage;

import com.gitee.huanminabc.fastboot.context.AppContext;
import com.gitee.huanminabc.fastboot.context.AppContextUtil;
import com.gitee.huanminabc.fastboot.load.stage.syncstage.AppLoadSyncStage;
import com.gitee.huanminabc.fastboot.load.stage.syncstage.ClassLoadContainerStage;
import com.gitee.huanminabc.fastboot.load.stage.syncstage.ProjectClassLoadStage;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: huanmin
 * @create: 2025-04-08 18:04
 **/
@Slf4j
public class SyncStageLauncher {
    public final static List<AppLoadSyncStage> appLoadStages = new ArrayList<>();
    static {
        //项目类加载
        appLoadStages.add(new ProjectClassLoadStage());
        //类生成对象加载到容器
        appLoadStages.add(new ClassLoadContainerStage());
    }

    public void load() {
        AppContextUtil.orderSort(appLoadStages);

        for (AppLoadStage appLoadStage : appLoadStages) {
            if (!appLoadStage.isRun()) {
                continue;
            }
            AppContext.taskTimeWatch.start(appLoadStage.getClass().getSimpleName()+"耗时");
            try {
                appLoadStage.run();
            } catch (Exception e) {
                log.error("SyncStageLauncher:", e);
                System.exit(1);
            } finally {
                AppContext.taskTimeWatch.stop();
            }
        }
    }
}
